#include <dlfcn.h>
#include <unistd.h>
#include <platform.h>
#include <pthread.h>
#include <sched.h>
#include <sys/resource.h>

unsigned long pd_get_ticks(void){
	struct timeval tv;
	struct timezone tz;
	gettimeofday(&tv, &tz);
	return (tv.tv_sec*1000 + tv.tv_usec/1000);
}

void pd_msleep(int ms){	
	struct timeval time;
	time.tv_sec = ms/1000;
	time.tv_usec  = (ms % 1000) * 1000;
	select(0,NULL,NULL,NULL,&time);
}

void *
	pd_dlopen(const char *filename, int flag){return dlopen(filename,!flag?RTLD_LAZY:flag);}

const char *
	pd_dlerror(void){return dlerror();}

void *
	pd_dlsym(void *handle, const char *symbol){return dlsym(handle,symbol);}

int 
	pd_dlclose(void *handle){return dlclose(handle);}


void pd_dump(const char *function,const char *data ,int size)
{
#define isprint(c)		(c>='!'&&c<='~')
//((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
		char* ptr;
		char digits[512] = {0};
		int i, j;		
		unsigned char *buf = (unsigned char*)data;
		if(function)
			UDK_PRINT("%s:%d\n",
						   function,size);

		
		for (i=0; i<size; i+=16) 
		{
		  ptr = &digits[0];
		  ptr+=sprintf(ptr,"%06x: ",i);
		  for (j=0; j<16; j++) 
				if (i+j < size)
				 ptr+=sprintf(ptr,"%02x ",buf[i+j]);
				else
				 ptr+=sprintf(ptr,"%s","   ");

		  ptr+=sprintf(ptr,"%s","  ");
				
		  for (j=0; j<16; j++) 
				if (i+j < size) 				
						ptr+=sprintf(ptr,"%c",isprint(buf[i+j]) ? buf[i+j] : '.');
		  *ptr='\0';
		  UDK_PRINT("%s\n",digits);
		}
}

